package stream;

import java.util.Optional;

class User{
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}
public class OptionalTest2 {
	/**
	 * 丑陋的代码
	 */
	public static String getName(User u) {
		if(u == null) 
			return "UnKnow";
		return u.getName();
	}
	/**
	 * 好看的代码
	 */
	public static String getNameAdvance(User u) {
		return Optional.ofNullable(u)
						.map(user -> u.getName())
						.orElse("UnKnow");
	}
	
	public static void main(String[] args) {
		
		User u = new User();
		u = null;
		System.out.println(getName(u));
		System.out.println(getNameAdvance(u));
	}

}
